#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

#define ARG_NUM_MAX 16

static char *argv[ARG_NUM_MAX];

char input[] = "hello world alex";
char *buf = input;

/**
 * @brief
 * strtok传入的参数需要是字符串数组，内部应该需要获取字符串长度吧。如果传入指针无法获取字符串长度。
 */
void test_strtok1()
{
    int i = 0;
    char *line1 = "hello world alex";
    char line2[] = "hello world alex";
    char *p;

    // p = strtok(line1, " "); // 错误
    p = strtok(line2, " "); // 正确
    while (p != NULL)
    {
        printf("%d, %s\n", ++i, p);
        p = strtok(NULL, " ");
    }
}

int get_cmds()
{
    char buffer[128];
    fgets(buffer, sizeof(buffer), stdin);
    // printf("%s\n", buffer);

    int argc = 0;
    while ((argv[argc] = strtok(buf, " ")) != NULL)
    {
        argc++;
        buf = NULL;
    }
    for (int i = 0; i < argc; ++i)
    {
        printf("%s \n", argv[i]);
    }

    return argc;
}

void test01()
{
    int argc = get_cmds();
}

int main(int argc, char *argv[])
{
    // test_strtok1();
    test01();
    return 0;
}